package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.ReturnListDao;
import com.atguigu.jxc.dao.ReturnListGoodsDao;
import com.atguigu.jxc.dao.SupplierDao;
import com.atguigu.jxc.dao.UserDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.ReturnList;
import com.atguigu.jxc.entity.ReturnListGoods;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.entity.User;
import com.atguigu.jxc.service.ReturnListGoodsService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ReturnListGoodsServiceImpl implements ReturnListGoodsService {

    @Autowired
    private ReturnListGoodsDao returnListGoodsDao;

    @Autowired
    private ReturnListDao returnListDao;

    @Autowired
    private SupplierDao supplierDao;

    @Autowired
    private UserDao userDao;

    @Override
    public ServiceVO save(ReturnList returnList, String returnListGoodsStr, String returnNumber) {
        ReturnList returnListVo = new ReturnList();
        returnListVo.setReturnNumber(returnNumber);
        returnListVo.setReturnDate(returnList.getReturnDate());
        returnListVo.setAmountPaid(returnList.getAmountPaid());
        returnListVo.setAmountPayable(returnList.getAmountPayable());
        returnListVo.setRemarks(returnList.getRemarks());
        returnListVo.setState(returnList.getState());
        returnListVo.setSupplierId(returnList.getSupplierId());
        returnListVo.setUserId(1);//TODO 哪哪都用的到，漂亮

        Supplier supplierById = supplierDao.getSupplierById(returnList.getSupplierId());
        String supplierName = supplierById.getSupplierName();
        returnListVo.setSupplierName(supplierName);
        User userById = userDao.getUserById(1);
        returnListVo.setTrueName(userById.getTrueName());
        returnListDao.save(returnListVo);

        Gson gson = new Gson();
        ReturnListGoods[] returnListGoods = gson.fromJson(returnListGoodsStr, ReturnListGoods[].class);
        for (ReturnListGoods returnListGood : returnListGoods) {
            ReturnListGoods returnListGoodsVo = new ReturnListGoods();
            returnListGoodsVo.setGoodsId(returnListGood.getGoodsId());
            returnListGoodsVo.setGoodsCode(returnListGood.getGoodsCode());
            returnListGoodsVo.setGoodsName(returnListGood.getGoodsName());
            returnListGoodsVo.setGoodsModel(returnListGood.getGoodsModel());
            returnListGoodsVo.setGoodsUnit(returnListGood.getGoodsUnit());
            returnListGoodsVo.setGoodsNum(returnListGood.getGoodsNum());
            returnListGoodsVo.setPrice(returnListGood.getPrice());
            returnListGoodsVo.setTotal(returnListGood.getTotal());
            returnListGoodsVo.setReturnListId(returnListVo.getReturnListId());
            returnListGoodsVo.setGoodsTypeId(returnListGood.getGoodsTypeId());
            returnListGoodsDao.save(returnListGoodsVo);
        }

        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }

    @Override
    public List<ReturnList> getList(String returnNumber, Integer supplierId, Integer state, String sTime, String eTime) {
        List<ReturnList> lists = returnListDao.getList(returnNumber, supplierId, state, sTime, eTime);
        for (ReturnList list : lists) {
            Integer supplierIdVo = list.getSupplierId();
            Supplier supplierById = supplierDao.getSupplierById(supplierIdVo);
            list.setSupplierName(supplierById.getSupplierName());
            Integer userId = list.getUserId();
            User userById = userDao.getUserById(userId);
            list.setTrueName(userById.getTrueName());
        }
        return lists;
    }

    @Override
    public List<ReturnListGoods> goodsList(Integer returnListId) {
        return returnListGoodsDao.goodsList(returnListId);
    }

    @Override
    public ServiceVO deleteByReturnListId(Integer returnListId) {

        //先判断有没有详情
        List<ReturnListGoods> goodsList = returnListGoodsDao.goodsList(returnListId);
        if (goodsList!=null){//详情有数据，循环删除
            for (ReturnListGoods returnListGoods : goodsList) {
                Integer returnListGoodsId = returnListGoods.getReturnListGoodsId();
                returnListGoodsDao.deleteById(returnListGoodsId);
            }
        }
        //走到这，该purchaseListId在详情表一定是没有数据的
        returnListDao.deleteById(returnListId);
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }

    @Override
    public ServiceVO updateState(Integer returnListId) {
        returnListDao.updateState(returnListId);
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }

    @Override
    public List<Map<String,Object>> count(String sTime, String eTime, Integer goodsTypeId, String codeOrName) {
        List<Map<String,Object>> mapList=returnListGoodsDao.count(sTime,eTime,goodsTypeId,codeOrName);
        return mapList;
    }
}
